package com.panshi.service.impl;


import com.panshi.mapper.IReplyMapper;
import com.panshi.service.IReplyService;
import com.panshi.vo.qa.Pages;
import com.panshi.vo.qa.Reply;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ReplyService implements IReplyService {

    @Autowired
    IReplyMapper replyMapper;

    @Override
    @Transactional(propagation= Propagation.REQUIRED)//spring事务注解型
    public boolean addReply(Reply reply) {
        try {
            replyMapper.addReply(reply);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    public Pages<Reply> inquirePagesReply(Pages pages) {
        List<Reply> list = replyMapper.inquirePagesReply(pages);
        System.out.println(list);
        Integer sum = replyMapper.inquireReplySum(pages);
        Pages<Reply> p = new Pages<>();
        p.setList(list);
        p.setSum(sum);
        return p;
    }

    @Override
    public Integer inquireReplySum(Pages pages) {
        Integer sum = replyMapper.inquireReplySum(pages);
        return sum;
    }

    @Override
    @Cacheable(value = "reply:inquireById",key = "#id")
    public Reply inquireById(int id) {
        Reply reply = replyMapper.inquireById(id);
        return reply;
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED)//spring事务注解型
    @CacheEvict(value = "reply:inquireById",key = "#id")
    public boolean praiseSumAnd1(int id) {
        try {
            replyMapper.praiseSumAnd1(id);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED)//spring事务注解型
    @CacheEvict(value = "reply:inquireById",key = "#id")
    public boolean praiseSumSubtract1(int id) {
        try {
            replyMapper.praiseSumSubtract1(id);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED)//spring事务注解型
    @CacheEvict(value = "reply:inquireById",key = "#id")
    public boolean trampleSumAnd1(int id) {
        try {
            replyMapper.trampleSumAnd1(id);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED)//spring事务注解型
    @CacheEvict(value = "reply:inquireById",key = "#id")
    public boolean trampleSumSubtract1(int id) {
        try {
            replyMapper.trampleSumSubtract1(id);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED)//spring事务注解型
    @CacheEvict(value = "reply:inquireById",key = "#id")
    public boolean deleteReply(int id) {
        try {
            replyMapper.deleteReply(id);
            return true;
        }catch (Exception e){
            return false;
        }
    }
}
